package com.atguigu.srb.core.service.impl;

import com.atguigu.srb.core.mapper.UserInfoMapper;
import com.atguigu.srb.core.pojo.entity.IntegralGrade;
import com.atguigu.srb.core.mapper.IntegralGradeMapper;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.service.IntegralGradeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * <p>
 * 积分等级表 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-09-13
 */
@Service
public class IntegralGradeServiceImpl extends ServiceImpl<IntegralGradeMapper, IntegralGrade> implements IntegralGradeService {

    @Resource
    UserInfoMapper userInfoMapper;
    @Override
    public BigDecimal getBorrowAmountByUserId(Long userId) {
        UserInfo userInfo = userInfoMapper.selectById(userId);
        //获取总结分
        Integer integral = userInfo.getIntegral();
        //根据总积分查借款额度
        QueryWrapper<IntegralGrade> integralGradeQueryWrapper
               = new QueryWrapper<>();
        integralGradeQueryWrapper.le("integral_start",integral)
                .gt("integral_end",integral);
        IntegralGrade integralGrade = baseMapper.selectOne(integralGradeQueryWrapper);
        if (null==integralGrade){
            return new BigDecimal("0");
        }
        return integralGrade.getBorrowAmount();
    }
}
